package com.zhuiyun.pm.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName mysqldump
 * @Description TODO
 * @Author lzy
 * @Date 2023/3/26 20:02yyyy-MM-dd-HH-mm-ss
 **/
@EnableScheduling
@Configuration
public class ExportSql {

    @Autowired
    private MysqldumpConfig mysqldumpConfig;

    @Scheduled(cron = "0 0 1 * * ?")
    public void downloadFile(){
        String exportPath = mysqldump();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
//        InputStream f = new FileInputStream(new File(exportPath));
//        response.reset();
//        response.setContentType("application/x-msdownload;charset=utf-8");
//        response.setHeader("Content-Disposition", "attachment;filename=" + sdf.format(new Date()) + ".sql");//下载文件的名称
//        ServletOutputStream sout = response.getOutputStream();
//        BufferedInputStream bis = null;
//        BufferedOutputStream bos = null;
//        try {
//            bis = new BufferedInputStream(f);
//            bos = new BufferedOutputStream(sout);
//            byte[] buff = new byte[2048];
//            int bytesRead;
//            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
//                bos.write(buff, 0, bytesRead);
//            }
//            bos.flush();
//            bos.close();
//            bis.close();
//        } catch (final IOException e) {
//            throw e;
//        } finally {
//            if (bis != null) {
//                bis.close();
//            }
//            if (bos != null) {
//                bos.close();
//            }
//        }

    }

    public void deleteBackupSql(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -7);
        String dataOld = sdf.format(c.getTime());
        String path = mysqldumpConfig.getExportPath() + mysqldumpConfig.getExportDatabaseName() + dataOld;
    }

    public String mysqldump() {
        Runtime runtime = Runtime.getRuntime();
        SimpleDateFormat sdf = new SimpleDateFormat("HH-mm-ss");
        SimpleDateFormat sdfRq = new SimpleDateFormat("yyyy-MM-dd");
        String exportPath = mysqldumpConfig.getExportPath() + mysqldumpConfig.getExportDatabaseName() + "/" + sdfRq.format(new Date()) + "/" + mysqldumpConfig.getExportDatabaseName() + sdf.format(new Date()) + ".sql";
        String command = getExportCommand(exportPath);
        File file = new File(exportPath);
        if (!file.getParentFile().exists()) {
            //不存在就创建文件夹
            file.getParentFile().mkdirs();
        }
        // 这里其实是在命令窗口中执行的 command 命令行
        try {
            runtime.exec(command);
            return exportPath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 得到导出数据的命令行语句
    private String getExportCommand(String exportPath) {
        StringBuffer command = new StringBuffer();
        String username = mysqldumpConfig.getUsername();// 用户名
        String password = mysqldumpConfig.getPassword();// 密码
        String host = mysqldumpConfig.getHost();// 导入的目标数据库所在的主机
        String port = mysqldumpConfig.getPort();// 使用的端口号
        String exportDatabaseName = mysqldumpConfig.getExportDatabaseName();// 导入的目标数据库的名称
        String MysqlPath = mysqldumpConfig.getMysqlPath(); //路径是mysql中
        // 注意哪些地方要空格，哪些不要空格
        command.append(MysqlPath).append("mysqldump -u").append(username).append(" -p").append(password)// 密码是用的小p，而端口是用的大P。
                .append(" -h").append(host).append(" -P").append(port).append(" ").append(exportDatabaseName)
                .append(" -r ").append(exportPath);
        return command.toString();
    }


}
